package com.maimao.info.test;

import com.alibaba.fastjson.JSON;
import com.maimao.core.constant.RedisKeys;
import com.maimao.info.InfoApplication;
import com.maimao.info.mapper.CouponMapper;
import com.maimao.info.mapper.RegionMapper;
import com.maimao.mg.repository.MgCouponMapper;
import com.maimao.model.info.Coupon;
import com.maimao.model.info.MgCoupon;
import com.maimao.model.info.Region;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2021/1/7 7:22 下午
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InfoApplication.class)
public class InfoText {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private MgCouponMapper mgCouponMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void createCouponMongoDoc() {
        Long shopId = 1L;
        Long goodsId = Long.valueOf("1341426379399954433");
        Query query = new Query();
        query.addCriteria(Criteria.where("startTime").gt(LocalDateTime.of(2021, 1, 7, 5, 17)));
   /*     query.addCriteria(org.springframework.data.mongodb.core.query.Criteria.where("shopId").is(shopId).and("deleted").is(0).and("endTime").gte(DateUtils.now())
                .orOperator(org.springframework.data.mongodb.core.query.Criteria.where("where").in(1, 2).and("goodsId").in(goodsId), Criteria.where("shopId").is(shopId).and("where").is(3)));*/
        List<MgCoupon> mgCoupons = mongoTemplate.find(query, MgCoupon.class);
        for (MgCoupon mgCoupon : mgCoupons) {
            System.out.println(mgCoupon);
        }
    }

    @Test
    public void queryGoodsCoupon() {
        List<Coupon> coupons = couponMapper.selectAll();
        List<MgCoupon> mgCoupons = new ArrayList<>(coupons.size());
        coupons.stream().forEach(coupon -> {
            MgCoupon mgCoupon = new MgCoupon();
            BeanUtils.copyProperties(coupon, mgCoupon);
            String goodsId = coupon.getGoodsId();
            if (!StringUtils.isEmpty(goodsId)) {
                mgCoupon.setGoodsId(JSON.parseObject(goodsId, List.class));
            }
            mgCoupons.add(mgCoupon);
        });
//        System.out.println(mgCoupons);
        mgCouponMapper.saveAll(mgCoupons);
    }

    @Test
    public void re() {
        List<Region> regionsTree = redisTemplate.opsForList().range(RedisKeys.REGION_TREE, 0, -1);
        if (regionsTree == null || regionsTree.size() <= 0) {
            regionsTree = regionMapper.treeData(0);
            redisTemplate.delete(RedisKeys.REGION_TREE);
            Region[] regionArr = new Region[regionsTree.size()];
            for (int i = 0; i < regionsTree.size(); i++) {
                regionArr[i] = regionsTree.get(i);
            }
            redisTemplate.opsForList().rightPushAll(RedisKeys.REGION_TREE, regionArr);

        }
    }

    @Test
    public void testcompareFields() {

        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(Long.valueOf("5063012314449985536"))).addCriteria(new Criteria() {
            @Override
            public Document getCriteriaObject() {
                Document document = new Document();
                document.put("$where", "this.takeCount<this.count");
                return document;
            }
        });
        List<MgCoupon> mgCoupons = mongoTemplate.find(query, MgCoupon.class);
        System.out.println(mgCoupons);
    }

    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
}
